package com.lill.workflow.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lill.common.component.transaction.CommonTransaction;
import com.lill.common.component.transaction.TM;
import com.lill.common.component.util.IdUtil;
import com.lill.workflow.dao.IWorkflowInfoDao;
import com.lill.workflow.model.WorkflowEmployeeModel;
import com.lill.workflow.model.WorkflowInfoModel;
import com.lill.workflow.service.IWorkflowInfoService;

/**
 * Service实现类
 *
 * @author lill
 * @date 2019-05-26 12:05:20
 */
@Service("workflowInfoService")
public class WorkflowInfoServiceImpl implements IWorkflowInfoService {
    @Autowired
    private IWorkflowInfoDao workflowInfoDao;

    public WorkflowInfoModel queryObject(String id) {
    	WorkflowInfoModel workflowInfo=workflowInfoDao.queryObject(id);
    	workflowInfo.setAuditEmployeeList(workflowInfoDao.selectAuditEmployee(id));
    	workflowInfo.setCopyEmployeeList(workflowInfoDao.selectCopyEmployee(id));
        return workflowInfo;
    }

	@Override
	public int selectCurrentSum(String id) {
		return workflowInfoDao.selectCurrentSum(id);
	}

	public List<WorkflowInfoModel> queryList(Map<String, Object> map) {
		if(null!=map&&!map.isEmpty()&&map.containsKey("onlyOne")&&map.containsKey("branchId")){
			map.remove("branchId");
		}
        return workflowInfoDao.queryList(map);
    }

	@Override
	public List<WorkflowInfoModel> queryListByOther(Map<String, Object> map) {
		return workflowInfoDao.queryListByOther(map);
	}

	public int queryTotal(Map<String, Object> map) {
        return workflowInfoDao.queryTotal(map);
    }

	@CommonTransaction({TM.flow})
    public int save(WorkflowInfoModel workflowInfo) {
    	String id=IdUtil.createIdbyUUID();
    	List<WorkflowEmployeeModel> auditEmployeeList=workflowInfo.getAuditEmployeeList();
    	if(null!=auditEmployeeList&&!auditEmployeeList.isEmpty()){
    		Map<String, Object> auditStepMap=new HashMap<String, Object>();
    		auditStepMap.put("workflowId", id);
    		WorkflowEmployeeModel auditEmployee=null;
    		List<WorkflowEmployeeModel> auditList=new ArrayList<WorkflowEmployeeModel>();
    		for (int i = 0; i < auditEmployeeList.size(); i++) {
    			auditEmployee=auditEmployeeList.get(i);
    			if(i!=(auditEmployeeList.size()-1)){
    				for (WorkflowEmployeeModel workflowEmployee : auditEmployeeList) {
    					if(i==workflowEmployee.getOrdernum()){
    						auditEmployee=workflowEmployee;
    						break;
    					}
    				}

    				for (WorkflowEmployeeModel nextEmployee : auditEmployeeList) {
    					if((i+1)==nextEmployee.getOrdernum()){
    						auditEmployee.setNextId(nextEmployee.getId());
    						break;
    					}
    				}
    			}
    			auditList.add(auditEmployee);
			}
    		auditStepMap.put("auditEmployee", auditList);

    		workflowInfoDao.saveAudit(auditStepMap);
    	}else{
    		return 0;
    	}

    	List<WorkflowEmployeeModel> copyEmployeeList=workflowInfo.getCopyEmployeeList();
    	if(null!=copyEmployeeList&&!copyEmployeeList.isEmpty()){
    		Map<String, Object> copyStepMap=new HashMap<String, Object>();
    		copyStepMap.put("workflowId", id);
    		List<String> copyEmployeeIdList=new ArrayList<String>();
    		for (WorkflowEmployeeModel copyEmployee : copyEmployeeList) {
    			copyEmployeeIdList.add(copyEmployee.getId());
			}
    		copyStepMap.put("copyEmployee", copyEmployeeIdList);
    		
    		workflowInfoDao.saveCopy(copyStepMap);
    	}
        workflowInfo.setId(id);
        return workflowInfoDao.save(workflowInfo);
    }
    
	@CommonTransaction({TM.flow})
	public int save(WorkflowInfoModel workflowInfo, boolean hasRepeat) {
		if(!hasRepeat){
			Map<String,Object>map = new HashMap<>();
	        map.put("companyId",workflowInfo.getCompanyId());
	        map.put("branchId",workflowInfo.getBranchId());
	        map.put("departId",workflowInfo.getDepartId());
	        map.put("code",workflowInfo.getCode());
	        map.put("status",1);
	        List<WorkflowInfoModel> workflowInfoList = queryList(map);
	        if(null!=workflowInfoList&&!workflowInfoList.isEmpty()){
	        	Map<String, Object> params=new HashMap<String, Object>();
	        	params.put("updateUser", workflowInfo.getCreateUser());
	        	params.put("updateTime", workflowInfo.getCreateTime());
	        	
	        	String[] ids=new String[workflowInfoList.size()];
	        	for (int i = 0; i < workflowInfoList.size(); i++) {
	        		ids[i]=workflowInfoList.get(i).getId();
				}
	        	params.put("ids", ids);
	        	workflowInfoDao.freezeLogicByMap(map);
	        }
		}
		return save(workflowInfo);
	}
    

    public int update(WorkflowInfoModel workflowInfo) {
    	List<WorkflowEmployeeModel> copyEmployeeList=workflowInfo.getCopyEmployeeList();
    	workflowInfoDao.deleteCopyCopyEmployee(workflowInfo.getId());
    	if(null!=copyEmployeeList&&!copyEmployeeList.isEmpty()){
    		Map<String, Object> copyStepMap=new HashMap<String, Object>();
    		copyStepMap.put("workflowId", workflowInfo.getId());
    		List<String> copyEmployeeIdList=new ArrayList<String>();
    		for (WorkflowEmployeeModel copyEmployee : copyEmployeeList) {
    			copyEmployeeIdList.add(copyEmployee.getId());
			}
    		copyStepMap.put("copyEmployee", copyEmployeeIdList);
    		
    		workflowInfoDao.saveCopy(copyStepMap);
    	}
        return workflowInfoDao.update(workflowInfo);
    }

    @Override
	public int deleteLogicByMap(Map<String, Object> map) {
		return workflowInfoDao.freezeLogicByMap(map);
	}

	@Override
	public List<WorkflowEmployeeModel> queryEmployeeFormal(HashMap<String, Object> paramsMap) {
		return workflowInfoDao.queryEmployeeFormal(paramsMap);
	}

	@Override
	public List<WorkflowEmployeeModel> queryEmployeeRenew(HashMap<String, Object> paramsMap) {
		return workflowInfoDao.queryEmployeeRenew(paramsMap);
	}

	@Override
	public WorkflowEmployeeModel queryEmployeeFormalByName(Map<String, Object> hashMap) {
		return workflowInfoDao.queryEmployeeFormalByName(hashMap);
	}

	@Override
	public List<WorkflowEmployeeModel> queryEmployeeFormalBoth(HashMap<String, Object> paramsMap) {
		return workflowInfoDao.queryEmployeeFormalBoth(paramsMap);
	}

	@Override
	public List<WorkflowEmployeeModel> queryEmployeeQuit(HashMap<String, Object> paramsMap) {
		return workflowInfoDao.queryEmployeeQuit(paramsMap);
	}

	@Override
	public WorkflowEmployeeModel queryFirstStep(String workflowId) {
		return workflowInfoDao.queryFirstStep(workflowId);
	}

	@Override
	public WorkflowEmployeeModel queryStepNext(Map<String, Object> mapNext) {
		return workflowInfoDao.queryStepNext(mapNext);
	}

	/**
	 * 根据流程编码查询所有审核人
	 * @param hashMap
	 * @return
	 */
	@Override
	public List<WorkflowEmployeeModel> querySteps(Map<String, Object> hashMap){
		return workflowInfoDao.querySteps(hashMap);
	}

}
